package ipdesk.model.report.channel.action;

import java.util.List;
import java.util.Map;

import ipdesk.model.report.channel.bean.ChannelInfoBean;
import ipdesk.model.report.channel.dao.ChannelInfoDao;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.opensymphony.xwork2.Preparable;

import ecg.util.FileService;
import ecg.util.PageUtil;

public class ChannelInfoSelectAction extends ActionSupport implements Preparable, ModelDriven<ChannelInfoBean>
{
	private ChannelInfoBean channelInfoBean;
	private int totalCount;
	private Map pageInfoDataMap;
	private String deskCode;
	private List<ChannelInfoBean> channelList;
	private ChannelInfoBean channelInfoDetail;
	
	FileService fileService = new FileService();
	
	public String execute() throws Exception
	{
		totalCount = ChannelInfoDao.getInstance().getDataTotalCount(channelInfoBean);
		
		pageInfoDataMap = PageUtil.getInstance().getPageInfoData(totalCount, channelInfoBean.getPageNumber());
		
		if(channelInfoBean.getDeskCode() != null || !"".equals(channelInfoBean.getDeskCode())){
			System.out.println("333");
			pageInfoDataMap.put("deskCode", channelInfoBean.getDeskCode());
			deskCode = channelInfoBean.getDeskCode();
		}
		
		if(channelInfoBean.getOrder() != null || !"".equals(channelInfoBean.getOrder())){
			pageInfoDataMap.put("order", channelInfoBean.getOrder());
		}
		
		channelList = ChannelInfoDao.getInstance().getPagingDataListForUser(pageInfoDataMap);
		
		return SUCCESS;
	}
	
	public String executeDetail() throws Exception
	{
		int channelSeq = channelInfoBean.getChannelSeq();
		System.out.println("invadeSeq :: "+channelSeq);
		
		channelInfoDetail = ChannelInfoDao.getInstance().getDetailData(channelSeq); 
		
		return SUCCESS;
	}

	public String executeModifyForm() throws Exception
	{
		int channelSeq = channelInfoBean.getChannelSeq();
		//System.out.println("brandSeq :: "+brandSeq);
		
		channelInfoDetail = ChannelInfoDao.getInstance().getDetailData(channelSeq); 
		
		return SUCCESS;
	}
	
	public String executeModify() throws Exception
	{
		// 데이터 업데이트, 파일 업로드 처리.
		String uplodaPathPrefix = getText("path.upload.prefix");
		String basePath = getText("path.upload.channel");
		String totalPath = uplodaPathPrefix + basePath;
		
		String uploadFileName = channelInfoBean.getUploadFileName();
		
		
		String realFileName = fileService.saveFile(channelInfoBean.getUpload(), totalPath, uploadFileName);
		
		// DB처리
		channelInfoBean.setUploadFileName(realFileName);
		channelInfoBean.setPathName(basePath);
		
		ChannelInfoDao.getInstance().channelInfoUpdate(channelInfoBean);
		
		return SUCCESS;
	}
	
	public String executeDelete() throws Exception
	{
		int channelSeq = channelInfoBean.getChannelSeq();
		System.out.println("seminarSeq :: "+channelSeq);
		
		if(channelInfoBean.getFileSeq() != null || !"".equals(channelInfoBean.getFileSeq())){
			String fileSeq = channelInfoBean.getFileSeq();
			
			// 1. 파일이 있다면, 파일 먼저 삭제
			String filePathPrefix = getText("path.upload.prefix");
			String fileName = channelInfoBean.getUploadFileName();
			String pathName = channelInfoBean.getPathName();
			
			fileService.deleteFile(filePathPrefix + pathName + fileName);
			
			// 2. file db에서 데이터 삭제
			ChannelInfoDao.getInstance().delFileData(fileSeq);
		}
		// 2. 글 삭제
		ChannelInfoDao.getInstance().delDetailData(channelSeq);	
		
		return SUCCESS;
	}
	
	public String executeDeleteFile() throws Exception
	{
		System.out.println("channelInfoBean.getFileSeq() :: "+channelInfoBean.getFileSeq());
		if(channelInfoBean.getFileSeq() != null || !"".equals(channelInfoBean.getFileSeq())){
			String fileSeq = channelInfoBean.getFileSeq();
			//System.out.println("channel seq :: "+fileSeq);
			// 1. 파일이 있다면, 파일 먼저 삭제
			String filePathPrefix = getText("path.upload.prefix");
			String fileName = channelInfoBean.getUploadFileName();
			String pathName = channelInfoBean.getPathName();
			
			fileService.deleteFile(filePathPrefix + pathName + fileName);
			
			// 2. file db에서 데이터 삭제			
			ChannelInfoDao.getInstance().delFileData(fileSeq);
		}
		
		return SUCCESS;
	}
	
	
	public int getTotalCount() {
		return totalCount;
	}

	public Map getPageInfoDataMap() {
		return pageInfoDataMap;
	}

	public String getDeskCode() {
		return deskCode;
	}

	public List<ChannelInfoBean> getChannelList() {
		return channelList;
	}

	public ChannelInfoBean getChannelInfoDetail() {
		return channelInfoDetail;
	}

	public void prepare() throws Exception {
		
		channelInfoBean = new ChannelInfoBean();
		
	}

	public ChannelInfoBean getModel() {
		
		return channelInfoBean;
	}
}

